package com.example.actionsignin.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.actionsignin.R;
import com.example.actionsignin.entity.ActionEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;

import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by 旭 on 2017/9/23/023.
 */

public class ActionAdapter extends BaseAdapter {

    private List<ActionEntity<UserEntity,ImagesEntity>> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public ActionAdapter(Context context, List<ActionEntity<UserEntity,ImagesEntity>> list)
    {
        this.mList = list;
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ActionEntity<UserEntity,ImagesEntity> actionEntity = mList.get(position);

        View view;
        ActionHolder actionHolder;
        if (convertView == null)
        {
            actionHolder = new ActionHolder();
            view = mLayoutInflater.inflate(R.layout.list_item_action,parent,false);
            actionHolder.imageView = (ImageView) view.findViewById(R.id.action_image);
            actionHolder.actionName = (TextView) view.findViewById(R.id.action_action_name);
            actionHolder.name = (TextView) view.findViewById(R.id.action_name);
            actionHolder.period = (TextView) view.findViewById(R.id.action_repeat);
            actionHolder.start = (TextView) view.findViewById(R.id.action_start_date);
            actionHolder.end = (TextView) view.findViewById(R.id.action_end_date);
            actionHolder.address = (TextView) view.findViewById(R.id.action_address);

            view.setTag(actionHolder);
            convertView = view;
        }else {
            actionHolder = (ActionHolder) convertView.getTag();
        }

        if (actionEntity.getActivityImages() != null)
        {
            if (actionEntity.getActivityImages().size() > 0)
            {
                ImagesEntity imagesEntity = actionEntity.getActivityImages().get(0);
                Glide.with(mContext).load(imagesEntity.getHttpPath()).into(actionHolder.imageView);
            }else {
                Glide.with(mContext).load(R.mipmap.camera).into(actionHolder.imageView);
            }
        }

        actionHolder.actionName.setText(actionEntity.getName());
        if (actionEntity.getUser() != null)
        {
            actionHolder.name.setText(actionEntity.getUser().getName());
        }
        actionHolder.period.setText(actionEntity.getPeriod());
        actionHolder.start.setText(actionEntity.getStart());
        actionHolder.end.setText(actionEntity.getEnd());
        actionHolder.address.setText(actionEntity.getAddress());

        return convertView;
    }


    class ActionHolder{
        public ImageView imageView;
        public TextView actionName;
        public TextView name;
        public TextView period;
        public TextView start;
        public TextView end;
        public TextView address;
    }
}
